<?php

/**
 * Subclass for representing a row from the 'remarks' table.
 *
 * 
 *
 * @package lib.model
 */ 
class Remarks extends BaseRemarks
{
    /**
     * 
     * returns true, if remark belongs to users created post (blog)
     */
    public function canUserEdit()
    {
        switch($this->getObjType())
        {
            case MyConstants::TYPE_BLOG:
                $obj = BlogPeer::retrieveByPK($this->getObjId());
                break;
            default:
                throw new Exception('object type not set yet');
                break;
        }
        if( ($obj->getUserId() == get_user_id()) || has_role_id(MyConstants::ROLE_ADMIN) )
        {
            return true;
        }
        return false;
    }
    /* @var $remark Remarks */
    /**
     * Returns array of remarks by object and type, filtered by status
     * 
     * @param int $objType
     * @param int $objId
     * @param int $status
     * @param int $limit - limit count returned objects
     */
    public static function getRemarksByObject($objType, $objId, $status = MyConstants::UNDEFINED_ID, $limit = 0 )
    {
        $c = new Criteria();
        $c->add(RemarksPeer::OBJ_TYPE, $objType);
        $c->add(RemarksPeer::OBJ_ID, $objId);
        $c->addDescendingOrderByColumn(RemarksPeer::CREATED_AT);
        if( $status > 0 )
        {
            $c->add(RemarksPeer::STATUS_ID, $status);
        }
        if( $limit > 0 )
        {
            $c->setLimit($limit);
        }
        $comments = RemarksPeer::doSelect($c);
        return $comments;
    }
    /**
     * 
     * remarks number
     * @param int $objType
     * @param int $objId
     * @param int $status
     */
    public static function getRemarksNumber($objType, $objId, $status = -1 )
    {
        $c = new Criteria();
        $c->add(RemarksPeer::OBJ_TYPE, $objType);
        $c->add(RemarksPeer::OBJ_ID, $objId);
        if( $status > 0 )
        {
            $c->add(RemarksPeer::STATUS_ID, $status);
        }
        $comments = RemarksPeer::doSelect($c);
        if( !$comments ) 
        {
            $number = 0;
        } else
        {
            $number = count($comments);
        }
        return $number;
    }
    public static function createNew($objId, $objType, $text, $userName, $userSurname, $userEmail, $rating, $status=false)
    {
        $remark = new Remarks();
        if( !$status )
        {
            $remark->setStatusId(MyConstants::STATUS_APPROVED);
        }else
        {
            $remark->setStatusId($status);
        }
        $remark->setUserId(get_user_id());
        $remark->setObjId($objId);
        $remark->setObjType($objType);
        $remark->setText($text);
        $remark->setUserName($userName);
        $remark->setUserSurname($userSurname);
        $remark->setUserEmail($userEmail);
        $remark->setRating($rating);
        $remark->save();
    }
}
